<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=7" />
	<title>物资采购与产品整合管理系统</title>
	<link href="${pageContext.request.contextPath }/css/main.css" rel="stylesheet" type="text/css" media="all" />
	<script type="text/javascript" src='<c:url value="/js/jquery-1.11.1.js"></c:url>'></script>
	<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
	<script type="text/javascript" language="javascript">
		$(function(){
			$("#btn_openParts").click(function(){
				//加载选择配件
				// $("#nestedDiv").load("selectParts.do");
				search('');
				$("#selectedPartsDiv").fadeIn();
			});

			$("#attachmentList :button").click(function () {
				$(this).parent().parent().remove();
			})

			//关闭窗口
			$("#close_selectedParts").click(function(){
				$("#selectedPartsDiv").fadeOut();
			});

			$("#btn_search").click(function(){
				search($("#partsname").val())
			});


			$("#btn_save").click(function(){
				// $("#coursesCreat").submit();
				$.ajax({
					url:"${pageContext.request.contextPath}/order.do?op=update",
					data:$("#coursesCreat").serialize(),
					dataType:"json",
					success:function (result) {
						alert(result.msg);
						if (result.succ){
							location.href="${pageContext.request.contextPath}/order.do?op=edit&orderid="+result.data+"";
						}
					}
				})
			});
			$("#btn_sbm").click(function(){
				// $("#coursesCreat").submit();
				$.ajax({
					url:"${pageContext.request.contextPath}/order.do?op=sbmit",
					data:$("#coursesCreat").serialize(),
					dataType:"json",
					success:function (result) {
						alert(result.msg);
						if (result.succ){
							location.href="${pageContext.request.contextPath}/order.do?op=query";
						}
					}
				})
			});

			$("#apply_selectedParts").click(function(){
				//获得选中的checkbox
				$("#partstable :checkbox:checked").each(function () {
					//选中checkbox的父元素（td）的下一个元素（td）的文本
					//获得选中的checkbox的配件id、配件名称、库存
					var partsid =$(this).parent().next().text();
					var partsname=$(this).parent().next().next().text();
					var partsreqcount=$(this).next().val();
					var flag=false;
					$("#attachmentList tr>input").each(function () {
						if ($(this).val()==partsid){
							flag=true
						}
					})
					if (!flag){
						$("#attachmentList").append("<tr>"+
								"<input type='hidden' name='partsid' value='"+partsid+"'>"+
								"<td>"+partsname+"</td>"+
								"<td><input type='text' name='ordercount' class='orderParts'></td>"+
								"<td>"+partsreqcount+"</td>"+
								"<td><input type='button' value='删除'/></td></tr>");
						$("#attachmentList :button").click(function () {
							$(this).parent().parent().remove();
						})
					}
				})
				$("#selectedPartsDiv").fadeOut();


			});

		});
		function  search(partsname) {
			$.ajax({
				url:"${pageContext.request.contextPath}/parts.do?op=queryajax",
				dataType:"json",
				data:"partsname="+partsname,
				success:function (result) {
					$("#partstable tr").remove(":not(:first)");

					//添加tr，td
					for (var i=0;i<result.length;i++){
						$("#partstable").append("<tr><td><input type='checkbox'><input type='hidden' value='"+result[i].partsreqcount+"'></td><td>"+result[i].partsid+"</td><td>"+result[i].partsname+"</td></tr>")
					}
				}
			});

		}

	</script>
</head>

<body class="content-pages-body">
<div class="content-pages-wrap">
	<div class="commonTitle">
		<h2>&gt;&gt; 订单信息修改</h2>
	</div>
	<form id="coursesCreat" name="coursesCreat" action="makeOrder.do" method="post">
		<table border="0" cellspacing="1" cellpadding="0" class="commonTable" >
			<tr>
				<td width="10%" align="right" class="title"><span class="required">*</span>订单序号：</td>
				<td width="15%" align="left"><input type="text" name="orderid" style="width:150px" value="${order.orderid}"></td>
				<td width="10%" align="right" class="title"><span class="required">*</span>订单编码：</td>
				<td width="15%" align="left"><input type="text" name="ordercode" style="width:150px" value="${order.ordercode}"></td>
				<td width="10%" align="right" class="title"><span class="required">*</span>订单保存日期：</td>
				<td width="15%" align="left">${order.orderdate}</td>
				<td width="10%" align="right" class="title"><span class="required">*</span>订单状态：</td>
				<td width="15%" align="left">
					<select id="orderflag" name="orderflag">
						<option value="1" selected>未提交</option>
						<option value="2">待审核</option>
					</select>
				</td>
			</tr>

		</table>
		<div align="left" style="margin-top:15px;margin-bottom:5px">
			<a href="javascript:void(0);" id="btn_openParts" title="选择原料" class="btnShort">选择配件</a>
		</div>
		<table width="90%" border="0" cellpadding="0" cellspacing="1" id="attachmentList" class="commonTable marginTopM">
			<tr>
				<th>配件名称</th>
				<th>进货数量</th>
				<th>库存数量</th>
				<th class="editColXS">操作</th>
			</tr>
			<c:forEach items="${orderdetail}" var="orderdetail">
				<tr>
					<input type="hidden" name="partsid" value="${orderdetail.partsid}">
                   <td>${orderdetail.partsname}</td>
					<td><input type="text"name="ordercount"value="${orderdetail.orderpartscount}"></td>
					<td>${orderdetail.partsreqcount}</td>
					<td><input type="button" value="删除" id="debtn"></td>
				</tr>
			</c:forEach>
		</table>
	</form>
</div>
<!--//commonTable-->
<div id="formPageButton">
	<ul>
		<li><a href="javascript:void(0)" title="保存" class="btnShort" id="btn_save">保存</a></li>
		<li><a href="javascript:void(0)" title="提交" class="btnShort" id="btn_sbm">提交</a></li>
		<li><a href="javascript:window.history.go(-1)" title="返回" class="btnShort">返回</a></li>
	</ul>
</div>
<!--//commonToolBar-->

<!-- 内置开始 -->
<div id="selectedPartsDiv" style="display:none;width:80%;height: 100%;background-color: #c07330;position: absolute;top: 100px;left: 100px">


	<div class="content-pages-wrap">
		<div class="commonTitle">
			<h2>&gt;&gt; 订单管理&nbsp;&gt;&gt;&nbsp;配件选择列表</h2>
		</div>
		<!-- <form id="form-search" name="form-search"  method="post"> -->
		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="commonTableSearch">
			<tr>
				<th align="right">配件名称：</th>
				<td ><input name="partsname" type="text" class="inputTextNormal" id="partsname" value="${params.partsName }"/></td>
				<td width="150" align="left" >
					<button id="btn_search">检索</button>
				</td>
				<td align="right">

				</td>
			</tr>
		</table>
		<!-- </form> -->
		<!--//commonTableSearch-->
		<div class="btnBar">
			<ul class="clearfix">
				<li><a href="javascript:void(0);" title="确定" class="btnLong" id="apply_selectedParts">确定</a></li>
				<li><a href="javascript:void(0);" title="关闭" id="close_selectedParts" class="btnLong">关闭</a></li>
			</ul>
		</div>
		<div id="nestedDiv" style="width:100%";height:100%>
			<table id="partstable" align="center" style="width: 80%;height: 100%;background-color: bisque" border="1">
				<th>选择</th>
				<th>配件编码</th>
				<th>配件名称</th>
			</table>

		</div>

	</div>

</div>
<!--//content pages wrap-->
</body>
</html>